package swings.panels;

import listener.JTextFieldHintListener;
import lombok.SneakyThrows;
import swings.buttons.CommonButton;
import swings.frames.AddRecordFrame;
import swings.labels.BgLabel;
import swings.others.CommonJTextField;
import utils.CommonUtils;
import utils.DataManageUtils;
import utils.ImageUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @version V1.0
 * @Title:
 * @Package
 * @Description:
 * @author: runoob
 * @date:
 */
public class ToolBarPanel extends CommonPanel {

    public static int x = 5;
    public static int y = 5;
    public static int width = RecordPanel.width;
    public static int height = 40;
    public static BgLabel bg = new BgLabel(x,y,width,height);
    private AddRecordFrame addRecordFrame;
    private CommonButton newBtn = new CommonButton(ImageUtils.newImg,5,0,40,40);
    private CommonButton clearBtn = new CommonButton(ImageUtils.clearImg,45,0,40,40);
    private CommonButton sureBtn = new CommonButton(ImageUtils.sureImg,5,0,40,40);
    private CommonButton cancelBtn = new CommonButton(ImageUtils.cancelImg,45,0,40,40);
    private CommonJTextField searchField = new CommonJTextField(50);
    private RecordPanel recordPanel;

    public ToolBarPanel(RecordPanel recordPanel) {
        this.recordPanel = recordPanel;
        addRecordFrame = new AddRecordFrame(recordPanel);
        newBtn.addActionListener(new newRecordFrame());
        clearBtn.addActionListener(new clearMode());
        sureBtn.addActionListener(new sureAction());
        cancelBtn.addActionListener(new cancelAction());
        sureBtn.setVisible(false);
        cancelBtn.setVisible(false);
        Font font = CommonUtils.getFont();
        JLabel search = new JLabel();
        search.setIcon(ImageUtils.search);
        search.setBounds(90,6,30,30);
        searchField.setFont(font);
        searchField.setBounds(120,7,165,29);
        searchField.setOpaque(false);
        searchField.setBorder(null);
        searchField.addKeyListener(new KeyAdapter(){
            @SneakyThrows
            public void keyPressed(KeyEvent e) {
                if(e.getKeyChar()==KeyEvent.VK_ENTER ){
                    doSearch();
                }
            }
        });
        searchField.addFocusListener(new JTextFieldHintListener(searchField, "Search"));
        JLabel fieldBg = new JLabel();
        fieldBg.setIcon(ImageUtils.common);
        fieldBg.setBounds(90,6,195,29);
        fieldBg.setBorder(BorderFactory.createLineBorder(CommonUtils.color, 1, true));
        this.add(newBtn);
        this.add(clearBtn);
        this.add(sureBtn);
        this.add(cancelBtn);
        this.add(search);
        this.add(searchField);
        this.add(fieldBg);
        this.setBounds(x,y,width,height);
    }

    /**
     * 手动刷新按钮监听器
     */
    private class newRecordFrame implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            addRecordFrame.setVisible(true);
        }
    }

    /**
     * 手动刷新按钮监听器
     */
    private class clearMode implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            //                Connection connectionNet = DataManageUtils.getConnection();
//                Statement statementNet = connectionNet.createStatement();
//                DataManageUtils.deleteAllRecords(statementNet);
//                statementNet.close();
//                connectionNet.close();
//                recordPanel.initRecords("");
            newBtn.setVisible(false);
            clearBtn.setVisible(false);
            sureBtn.setVisible(true);
            cancelBtn.setVisible(true);
            recordPanel.isMultipleChoice = true;
            recordPanel.multipleChoiceMap.put(recordPanel.chosenId, recordPanel.chosenId);
        }
    }

    /**
     * 手动刷新按钮监听器
     */
    private class sureAction implements ActionListener {
        @SneakyThrows
        @Override
        public void actionPerformed(ActionEvent e) {
            doSure();
            doCancel();
        }
    }

    /**
     * 手动刷新按钮监听器
     */
    private class cancelAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            doCancel();
        }
    }

    private void doSure() throws SQLException, ClassNotFoundException, IOException {
        Connection connection = DataManageUtils.getConnection();
        Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        Set<Map.Entry<Integer, Integer>> entries = recordPanel.multipleChoiceMap.entrySet();
        for (Map.Entry<Integer, Integer> entry : entries) {
            Map<String,Object> valueMap = new HashMap();
            valueMap.put("id", entry.getValue());
            DataManageUtils.deleteRecord(statement, valueMap);
        }
        // 关闭链接
        statement.close();
        connection.close();
        recordPanel.initRecords("");
    }

    private void doCancel(){
        newBtn.setVisible(true);
        clearBtn.setVisible(true);
        sureBtn.setVisible(false);
        cancelBtn.setVisible(false);
        recordPanel.isMultipleChoice = false;
        recordPanel.multipleChoiceMap.clear();
        recordPanel.refreshRecordList();
    }

    /**
     * 手动刷新按钮监听器
     */
    private void doSearch() throws SQLException, IOException, ClassNotFoundException {
        recordPanel.initRecords(searchField.getText());
    }

}
